package vip.web3.api.service.impl.base;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import vip.web3.admin.common.utils.Query;
import vip.web3.admin.sys.entity.sys.SysConfigEntity;
import vip.web3.api.constans.RedisConstant;
import vip.web3.api.dao.base.PlatformConfigDao;
import vip.web3.api.dao.biz.OrderDao;
import vip.web3.api.dao.biz.RecordDao;
import vip.web3.api.entity.base.PlatformConfig;
import vip.web3.api.entity.biz.UserEntity;
import vip.web3.api.service.base.PlatformConfigService;
import vip.web3.api.service.base.SysConfigApiService;
import vip.web3.common.utils.PageUtils;
import vip.web3.common.utils.RedisUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * PlatformConfigServiceImpl
 *
 * @author zhaojiafan
 * @date 2024/10/16
 */
@Service
public class PlatformConfigServiceImpl extends ServiceImpl<PlatformConfigDao, PlatformConfig> implements PlatformConfigService {
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private PlatformConfigDao configDao;
    @Resource
    private SysConfigApiService sysConfigApiService;
    @Resource
    private OrderDao orderDao;
    @Resource
    private RecordDao recordDao;


    @Override
    public Integer add(PlatformConfig platformConfig) {
        redisUtils.set(RedisConstant.PLATFORM_CONFIG, JSONObject.toJSONString(platformConfig));
        return configDao.insert(platformConfig);
    }

    @Override
    public PlatformConfig selectOne() {
        String apkUrl = sysConfigApiService.getValue("apkUrl");
        String ipaUrl = sysConfigApiService.getValue("ipaUrl");
        String wgtUrl = sysConfigApiService.getValue("wgtUrl");
        String apkUpdate = sysConfigApiService.getValue("apkUpdate");
        String ipaUpdate = sysConfigApiService.getValue("ipaUpdate");
        String wgtUpdate = sysConfigApiService.getValue("wgtUpdate");
        Integer apkVersion = null == sysConfigApiService.getValue("apkVersion") ? 0 : Integer.parseInt(sysConfigApiService.getValue("apkVersion"));
        Integer ipaVersion = null == sysConfigApiService.getValue("ipaVersion") ? 0 : Integer.parseInt(sysConfigApiService.getValue("ipaVersion"));
        String remark = sysConfigApiService.getValue("remark");
        String apkVersionValue = sysConfigApiService.getValue("apkVersionValue");
        String ipaVersionValue = sysConfigApiService.getValue("ipaVersionValue");
        PlatformConfig platformConfig = new PlatformConfig();

        platformConfig.setApkUrl(apkUrl);
        platformConfig.setIpaUrl(ipaUrl);
        platformConfig.setWgtUrl(wgtUrl);
        platformConfig.setApkUpdate(apkUpdate);
        platformConfig.setIpaUpdate(ipaUpdate);
        platformConfig.setWgtUpdate(wgtUpdate);
        platformConfig.setApkVersion(apkVersion);
        platformConfig.setIpaVersion(ipaVersion);
        platformConfig.setRemark(remark);

        platformConfig.setIpaVersionValue(apkVersionValue);
        platformConfig.setApkVersionValue(ipaVersionValue);

        return platformConfig;


    }

    private static String convertToDottedVersion(Integer param) {
        // 假设版本号长度不会超过7位数字
        String version = param.toString();
        if (version.length() > 7) {
            throw new IllegalArgumentException("Version string is too long.");
        }
        // 每1位数字用点分隔
        int segmentLength = 1;
        StringBuilder dottedVersion = new StringBuilder();
        for (int i = 0; i < version.length(); i += segmentLength) {
            if (i > 0) {
                dottedVersion.append(".");
            }
            dottedVersion.append(version.substring(i, Math.min(i + segmentLength, version.length())));
        }
        return dottedVersion.toString();
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PlatformConfig> page = this.page(
                new Query<PlatformConfig>().getPage(params),
                new QueryWrapper<PlatformConfig>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String,Object> queryData() {
        Double orderSum = orderDao.orderSum();
        Double recordSum = recordDao.recordSum();
        return new HashMap<>(){{
            put("orderSum",orderSum);
            put("recordSum",recordSum);
        }};
    }

}
